package com.xjtu.project.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjtu.project.mapper.SizeMapper;
import com.xjtu.project.model.domain.Shoe;
import com.xjtu.project.model.domain.Size;
import com.xjtu.project.model.dto.ShoeDTO;
import com.xjtu.project.model.dto.ShoeIfDTO;
import com.xjtu.project.model.dto.shoePageQuery;
import com.xjtu.project.model.vo.ShoeCategoryVO;
import com.xjtu.project.model.vo.ShoeCnVO;
import com.xjtu.project.model.vo.ShoeVO;
import com.xjtu.project.service.ShoeService;
import com.xjtu.project.mapper.ShoeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author 86185
* @description 针对表【shoe】的数据库操作Service实现
* @createDate 2025-07-04 18:26:18
*/
@Service
public class ShoeServiceImpl extends ServiceImpl<ShoeMapper, Shoe>
    implements ShoeService{
    @Autowired
    private ShoeMapper shoeMapper;
    @Autowired
    private SizeMapper sizeMapper;


    @Override
    public boolean updateByIdMy(Shoe shoe) {
        shoe.setUpdateTime(LocalDateTime.now());
        boolean b = shoeMapper.updateByIdMy(shoe);
        return b;
    }

    @Override
    public boolean addShoeMy(ShoeDTO shoe) {
        shoe.setCreateTime(LocalDateTime.now());
        shoe.setUpdateTime(LocalDateTime.now());
        shoeMapper.addShoeMy(shoe);
        List<Integer> lists = shoe.getShoeSize();
        List<Size> sizes = new ArrayList<>();
        for (Integer list : lists) {
            sizes.add(new Size().setSize(list).setShoeId(shoe.getId()));
        }
        boolean b = sizeMapper.addShoeSizeMy(sizes);
        return b;
    }

    @Override
    public ShoeVO getByIdMy(Long id) {
        ShoeVO shoeVO = shoeMapper.selectByIdMy(id);
        return shoeVO;
    }

    @Override
    public List<ShoeCategoryVO> getByCategory() {
        return shoeMapper.selectByCategory();
    }

    @Override
    public boolean updateByStatus(Integer status,Integer id) {
        boolean b = shoeMapper.updateByStatus(status,id);
        return b;
    }

    @Override
    public Page<Shoe> selectPagedMy(ShoeIfDTO shoe) {
        Page<Shoe> page = new Page<>(shoe.getPage(),shoe.getPageSize());
        Page<Shoe> page1 = lambdaQuery().like(shoe.getName() != null, Shoe::getName, shoe.getName())
                .eq(shoe.getCategoryId() != null, Shoe::getCategoryId, shoe.getCategoryId())
                .eq(shoe.getStatus() != null, Shoe::getStatus, shoe.getStatus()).page(page);
        return page1;
    }

    @Override
    public Page<Shoe> getPage(shoePageQuery shoePage) {
        Page<Shoe> page = new Page<>(shoePage.getPage(), shoePage.getPageSize());
        Page<Shoe> p = lambdaQuery().eq(shoePage.getCategoryId()!=null,Shoe::getCategoryId, shoePage.getCategoryId()).eq(shoePage.getStatus()!=null,Shoe::getStatus, shoePage.getStatus()).like(shoePage.getName()!=null,Shoe::getName, shoePage.getName()).page(page);
        return p;
    }

    @Override
    public Page<Shoe> selectByCategoryId(ShoeIfDTO shoe) {
        Page<Shoe> page = new Page<>(shoe.getPage(), shoe.getPageSize());
        Page<Shoe> page1 = lambdaQuery().eq(Shoe::getCategoryId, shoe.getCategoryId()).page(page);
        return page1;
    }

    @Override
    public List<ShoeCnVO> getByCategoryId(Integer categoryId) {
        return shoeMapper.getByCategoryId(categoryId);
    }

    @Override
    public List<ShoeCnVO> getByName(String name) {
        return shoeMapper.getByName(name);
    }
}




